
INS_DIR	:= ../../bin/

CFLAGS 		+= -Wall -fPIC -Werror -g -w -DDEBUG=1 -fmessage-length=0 -O2
CFLAGS		+= -I../include -DHOST

CXXFLAGS	= $(CFLAGS)

LDFLAGS		+= -Wl,--as-needed -lz
LDFLAGS		+= -L$(INS_DIR)

LIB_SRCS := \
    make_ext4fs.c \
    ext4fixup.c \
    ext4_utils.c \
    allocate.c \
    contents.c \
    extent.c \
    indirect.c \
    sha1.c \
    wipe.c \
    crc16.c \
    ext4_sb.c

AP1_SRC := make_ext4fs_main.c canned_fs_config.c
AP2_SRC := ext4fixup_main.c

LIB_OBJS := $(patsubst %.c,%.o,$(LIB_SRCS))
AP1_OBJS := $(patsubst %.c,%.o,$(AP1_SRC))
AP2_OBJS := $(patsubst %.c,%.o,$(AP2_SRC))

ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
LIB	:= libext4.so
LDFLAGS		+= -Wl,-rpath=.:./bin -lsparse -lpcre
APLDFLAGS	:= -lext4 -lselinux
else
LIB	:= libext4.a
APLDFLAGS	:= -Wl,-Bstatic -lselinux -lsepol -lext4 -lsparse -lpcre -Wl,-Bdynamic
endif

AP1	:= make_ext4fs
AP2	:= ext4fixup

TARGET	:= $(LIB) $(AP1) $(AP2)

all:$(TARGET)

$(LIB):$(LIB_OBJS)
ifneq ($(strip $(TOOLS_BUILD_SHARED)),)
	$(CC) -o $@ $^ -shared $(LDFLAGS)
	$(STRIP) $@
else
	$(AR) rc $@ $^
endif
	install -m 755 $@ $(INS_DIR)

$(AP1):$(AP1_OBJS)
	$(CC) -o $@ $^ $(APLDFLAGS) $(LDFLAGS)
	$(STRIP) $@

$(AP2):$(AP2_OBJS)
	$(CC) -o $@ $^ $(APLDFLAGS) $(LDFLAGS)
	$(STRIP) $@

%.o:%.c
	$(CC) $(CFLAGS) -c -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o $@ $<

%.o:%.cpp
	$(CXX) $(CXXFLAGS) -c -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o $@ $<

install:
	install -m 755 $(AP1) $(INS_DIR)
	install -m 755 $(AP2) $(INS_DIR)
	install -m 755 mkuserimg.sh $(INS_DIR)

clean:
	-rm -rf $(LIB_OBJS) $(AP1_OBJS) $(AP2_OBJS) *.d $(TARGET)

.PHONY: all clean install install_lib
